package com.android.gtravelling;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.dto.ProvincesDTO;

public class VietTravelActivity extends Activity {
	private ImageView imgView;
	TextView txtName;
	TextView txtContent;
	int expand = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.province_info);
		imgView = (ImageView) findViewById(R.id.imgProvince);
		txtName = (TextView) findViewById(R.id.province_name);
		txtContent = (TextView) findViewById(R.id.txtContent);

		//Region shows the action of button Read More
		txtContent.setLines(5);
		
		/******************************************************
		 * Show map
		 *******************************************************/
		Button btnShowMap=(Button)findViewById(R.id.btnShowMap);
		OnClickListener btnShowMapListener=new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				 
				// TODO Auto-generated method stub
				Intent i = new Intent (v.getContext(),ShowMapOfPlace.class);//
				i.putExtra(Intent.EXTRA_TEXT, String.valueOf(txtName.getText()+", Viet Nam"));
        		startActivity(i);
			}
		};
		btnShowMap.setOnClickListener(btnShowMapListener);
		//===================================================
		Button btnMore = (Button) findViewById(R.id.btnDetails);
		OnClickListener btnMoreListen = new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// txtContent.setLines(20);
				if (expand == 0) {
					int height_in_pixels = txtContent.getLineCount()
							* txtContent.getLineHeight(); // approx height text
					txtContent.setHeight(height_in_pixels);
					Button btn = (Button) v;
					btn.setText("Back");
					expand = 1;
				} else {
					txtContent.setLines(5);
					Button btn = (Button) v;
					btn.setText("Xem Thêm");
					expand = 0;
				}

			}
		};
		btnMore.setOnClickListener(btnMoreListen);
		//====================================================
		/******************************************************
		 * Ngày 23/3/2012
		 * Lê Xuân Ninh
		 * Xem các Places trong Tỉnh
		 *******************************************************/
		
		Button btnPlace=(Button)findViewById(R.id.btnPlaces);
		btnPlace.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String strProvinces =  txtName.getText().toString();
				// TODO Auto-generated method stub
				Intent iAdd = new Intent(v.getContext(), PlaceList.class);
				startActivity(iAdd);
			}
		});
		//====================================================
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			// Get data via the key
			String value1 = extras.getString(Intent.EXTRA_TEXT);
			if (value1 != null) {

				WebserviceCaller caller = new WebserviceCaller();
				ProvincesDTO prov = new ProvincesDTO();
				prov = caller.selectOneProvince(Integer.valueOf(value1));
				//System.out.println(prov.getName());

				txtName.setText(prov.getName());
				txtContent.setText(prov.getDescription());
				String imgURL = prov.getImgURL();
				//System.out.println(imgURL);
				if (imgURL == null) {
					imgView.setImageResource(R.drawable.cui_bap);
				} else {
					imgView.setImageResource(R.drawable.cui_bap);
//					InputStream is = null;
//					try {
//						is = fetch(imgURL);
//					} catch (Exception e) {
//
//					}
//
//					Drawable drawable = Drawable.createFromStream(is, "src");
//					imgView.setImageDrawable(drawable);
				}
			}
		}
	}

	private InputStream fetch(String urlString) throws MalformedURLException,
			IOException {
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpGet request = new HttpGet(urlString);
		HttpResponse response = httpClient.execute(request);
		return response.getEntity().getContent();
		
	}
}
